import java.util.Stack;

public class Sort {
    //插入排序

    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (tmp < array[j]) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //希尔排序
    public void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }
    public void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if (tmp < array[j]) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }


    //单向选择排序
    public void selectSort(int[] array) {
        int minIndex;
        for (int i = 0; i < array.length; i++) {
            minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }


    //双向选择排序selectSort2与3都是
    public void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            //minIndex和maxIndex 的初始值并不关键，只要范围在该数组内就行
            int minIndex = left;
            int maxIndex = left;
            //注意内层循环的条件，一定是i <= right
            for (int i = left; i <= right ; i++) {
                if(array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            //核心问题：索引冲突，在执行第一个swap的同时，下标的改变可能影响下一个swap
            if(maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    public void selectSort3(int[] array) {
       //类比selectSort2
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            //为了说明minIndex和maxIndex 的初始值并不关键，与selectSort2不同
            int minIndex = left;
            int maxIndex = right;
            for (int j = left; j <= right; j++) {
                if (array[j] > array[maxIndex]) {
                    maxIndex = j;
                }
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, minIndex, left);
            if (maxIndex == left) {
                // 处理索引冲突，依然是写在第一个swap后
                maxIndex = minIndex;
            }
            swap(array, maxIndex, right);
            left++;
            right--;
        }
    }

    //堆排序
    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0 ; parent--) {
            siftDown(array,parent,array.length);
        }
    }

    private void siftDown(int[] array,int parent,int end) {
        int child = 2*parent+1;
        while (child < end) {
            if(child + 1 < end && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    // 快速排序
    public void quickSor(int[] array) {
        quick(array,0,array.length-1);
    }
    public void quick(int[] array,int left,int right) {
        if (left >= right) {
            return;
        }
        // key是对快排进行的优化
        if (right - left <= 10) {
        // 10只是示范，该数值可以改变，选取合适，插入排序可以有效优化时空复杂度
            insertSortRange(array,left,right);
            return;
        }
        // 三数取中法优化时空复杂度
        int index = midOfThree(array,left,right);

        swap(array,index, left);
        int pivot = partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    //自定义开头结尾的插入排序，用于优化快速排序

    private static void insertSortRange(int[] array,int begin,int end) {
        for (int i = begin+1; i <= end; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= begin ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //  三数取中法
    public int midOfThree(int[] array,int start,int end) {
        int mid = (start + end)/2;
        if (array[start] > array[end]) {
            if(array[mid] > array[start]) {
                return start;
            }else if(array[mid] > array[end]) {
                return mid;
            }else {
                return end;
            }
        } else {
            if(array[mid] > array[start]) {
                return start;
            }else if(array[mid] < array[end]) {
                return end;
            }else {
                return mid;
            }
        }

    }

   //Hoare法
    public int partition(int[] array,int left,int right) {
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= array[i]) {
                right--;
            }
            while (left < right && array[left] <= i) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    // ！！！挖坑法和Hoare法差距不大注意区分
    public int partition2(int[] array,int left,int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= array[i]) {
                right--;
            }
            array[i] = array[right];

            while (left < right && array[left] <= i) {
                left++;
            }
            array[right] = tmp;
            i++;
        }
        swap(array,left,i);
        return left;
    }

    //！！！前后指针法，易忘
    public int partition3(int[] array,int left,int right) {
        int i = left;
        int j = i+1;
        while (j < right) {

            if (array[j] < array[left] && array[++i] != array[j]) {
                swap(array,j,i);
            }
            j++;
        }
        swap(array,i,left);
        return i;
    }
    // !!!!快速排序非递归执行易忘
    public void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int piovt = partition(array,left,right);
        if(piovt - 1 > left) {
            stack.push(left);
            stack.push(piovt-1);
        }
        if(piovt + 1 < right) {
            stack.push(piovt+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            piovt = partition(array,left,right);
            if(piovt - 1 > left) {
                stack.push(left);
                stack.push(piovt-1);
            }
            if(piovt + 1 < right) {
                stack.push(piovt+1);
                stack.push(right);
            }
        }
    }



    // 归并排序

    public void mergeSortFunc(int[] array,int left,int right){
        if(left >= right) return;
        int mid = (left+right)/2;
        mergeSortFunc(array,left,mid);
        mergeSortFunc(array,mid +1 ,right);
        merge(array,left,right,mid);
    }

    // !!! 易错
    public void merge(int[] array, int left, int right, int mid) {
        int s1 = left;
        int s2 = mid + 1;
        int[] tmpArr = new int[right - left + 1];
        int k = 0;
        // ！！！！注意循环结束条件
        while (s1 <= mid && s2 <= right) {
            if (array[s1] >= array[s2]) {
                tmpArr[k++] = array[s2++];
            } else {
                tmpArr[k++] = array[s1++];
            }
        }

        // ！！！！处理左右两数组不等长情况
        while (s1 <= mid) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= right) {
            tmpArr[k++] = array[s2++];
        }
        // ！！！！此处易错

        for (int i = 0; i < tmpArr.length; i++) {
            array[i+left] = tmpArr[i];
        }
    }

    public void mergeSort(int[] array) {
        mergeSortFunc(array,0,array.length-1);
    }



    // 非递归实现归并排序
    public void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2*gap) {
                int left = i;
                int mid =left+gap-1;
                int right = mid+gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }
            gap *= 2;
        }
    }
    // 计数排序
    public void countSort(int[] array) {
        int minVal = array[0];
        int maxVal = array[0];
        //1、求当前数组的最大值  和  最小值
        for (int i = 1; i < array.length; i++) {
            if(array[i] < minVal) {
                minVal = array[i];
            }
            if(array[i] > maxVal) {
                maxVal = array[i];
            }
        }
        //2.跟进最大值 和 最小值 来确定数组的大小
        int[] count = new int[maxVal-minVal+1];

        //3、遍历原来的数组 开始计数
        for (int i = 0; i < array.length; i++) {
            count[array[i]-minVal]++;
        }

        //4、遍历计数cout 把 当前元素 写回 array
        int index = 0;//重新表示array数组的下标
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }

    public void swap(int[] array,int a,int b) {
        int tmp = array[a];
        array[a] = array[b];
        array[b] = tmp;
    }
}

